<?php


class Type
{
    function __construct($db,$req)
    {
        $this->db = $db;
        $this->req = $req;
    }
    function json($sta,$data){
        return json_encode(array('status'=>$sta,'msg'=>$data));
    }
    function add(){
        date_default_timezone_set('prc');
        $name = $this->req['name'];
        $time = date('Y-m-d H:i:s',time());
        $r = $this->db->query('insert into type(name,time,count) values(?,?,?)',$name,$time,0);
        echo $this->json(200,'增加成功');
    }

    function find(){
        $r = $this->db->query('select * from type order by id desc ');
        echo $this->json(200,$r);
    }
    function del(){
        @ $id = $this->req['id'];
        if(empty($id)){
          exit($this->json(500,'必须传入id'));
        }else{
            $this->db->query('delete from type where id=?',$id);
            echo $this->json(200,'删除成功');
        }
    }
    function edit(){
        date_default_timezone_set('prc');
        $name = $this->req['name'];
        $id = $this->req['id'];
        $time = date('Y-m-d H:i:s',time());
        $r = $this->db->query('update type set name=?,time=? where id=?',$name,$time,$id);
        echo $this->json(200,'编辑成功');
    }
}